/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import jdbcmanager.app.*;
import jdbcmanager.swing.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * This class is a node that contains either Schemas (if the DBMS support them), or following 
 * child nodes:<br>
 * <ul>
 *   <li>NodeTable
 *   <li>NodePorcedure
 *   <li>NodeDiagram
 * </ul>
 *
 * @author Francisco Morero Peyrona
 */

final class NodeCatalog extends JdbcTreeNode
{
   NodeCatalog( String sCaption )
   {
      super( icon_tree_catalog, new DbEntityDescriptor( sCaption, null, sCaption ) );

      // Set Invalid Actions
      setOpenable( false );
   }

   // This class is always initialised, because when Catalogs are created, either Schemas or 
   // Tables, Procedures and Diagrams node branches are automatically added to Catalog node.
   boolean isInitialised()
   {
      return true;
   }

   // I prefer to show a msg explaining why can not perform the action rather than disableing the icon
   boolean new_()
   { 
      boolean bSuccess = false;
      
      if( DbUtil.getAgentDBMS().getAgentSQL().allowsCatalogCreation() )
         bSuccess = SqlUtil.createCatalog( DbUtil.getConnection( getCatalog() ) );
      else
         blameSomeOne( "create" );
         
      return bSuccess;
   }

   void open()
   {
      // Redefined because was declared abstract in parent, but nothing to do
   }

   // I prefer to show a msg explaining why can not perform the action rather than disableing the icon
   boolean edit()
   {
      boolean bSuccess = false;
      
      if( DbUtil.getAgentDBMS().getAgentSQL().allowsCatalogRename() )
         bSuccess = SqlUtil.renameCatalog( DbUtil.getConnection( getCatalog() ), getCatalog() );
      else
         blameSomeOne( "rename" );
         
      return bSuccess;
   }
   
   // I prefer to show a msg explaining why can not perform the action rather than disableing the icon
   // this.frame is always null because there is no frame associated with catalogs
   boolean rename( String sNewName )
   {
      boolean bSuccess = false;
      
      if( DbUtil.getAgentDBMS().getAgentSQL().allowsCatalogRename() )
         bSuccess = SqlUtil.renameCatalog( DbUtil.getConnection( getCatalog() ), getCatalog(), sNewName );
      else
         blameSomeOne( "rename" );
   
      return bSuccess;
   }
   
   // I prefer to show a msg explaining why can not perform the action rather than disabling the icon
   // this.frame is always null because there is no frame associated with catalogs
   boolean delete()
   {
      boolean bSuccess = false;
      
      if( DbUtil.getAgentDBMS().getAgentSQL().allowsCatalogCreation() )
         bSuccess = SqlUtil.dropCatalog( DbUtil.getConnection( getCatalog() ), getCatalog() );
      else
         blameSomeOne( "delete" );
      
     return bSuccess;
   }

   // This node only can contain children
   void info()
   {
      String  sTitle = DbUtil.getAgentDBMS().getAgentMetaData().getCatalogTerm() +" Information"; 
      
      JxDialog dialog = new JxDialog( sTitle );
               dialog.getContentPane().add( new Info4Catalog( getName() ) );
               dialog.setVisibleButtonPanel( false );
               dialog.pack();
               dialog.center();
               dialog.setVisible( true );
   }
   
   //-------------------------------------------------------------------------//
   
   private void blameSomeOne( String sAction )
   {
      String sMsg;
      
      if( DbUtil.getAgentDBMS().getAgentSQL().getClass().getName().endsWith( "AgentSQLDefault" ) )
         sMsg = "The operation you requested can not be performed, there are several\n"+
                "possible reasons.\n"+
                "On one side, you are using the default SQL agent, what means that most\n"+
                "probably there is not an specific agent yet for your DBMS: this can be\n"+
                "solved easily: if you are interested in creating an SQL Agent please\n"+
                "contact with "+ App.sAPP_NAME +" team.\n\n"+
                "The reason could also be that either the JDBC driver you are using,\n"+
                "the DBMS or both do not allow to perform this operation.";
      else
         sMsg = "Most probably, either the JDBC driver you are using,\n"+
                "the DBMS or both do not allow to perform this operation.";
             
      App.showMessage( "Can not "+ sAction +" "+ DbUtil.getAgentDBMS().getAgentMetaData().getCatalogTerm(), sMsg );
   }
}